'use strict';
import Base from './base.js';

export default class extends Base {
	// 获取会员等级列表
	async vipgradelistAction(){
		let orid = this.userinfo.departmentid;
		let data = await this.model('or_guest_vipgrade').where('departmentid='+orid+' or departmentid=0').order('vipgrade ASC').select();
		return this.json(data);
	}

	// 添加会员等级
	async addvipgradeAction(){
		let data = this.post();
		let orid = this.userinfo.departmentid;
		let addtime = Math.round(new Date().valueOf()/1000)

		let repeat = await this.model('or_guest_vipgrade').where({vipgrade: data.vipgrade, departmentid: orid, id:['!=', data.id]}).find();
		if (!think.isEmpty(repeat)) {
			return this.fail('会员等级重复');
		}

		if (data.id*1) {
			data.addtime = addtime;
			let addvip = await this.model('or_guest_vipgrade').update(data);
			return addvip?this.success('更改成功'):this.fail('更改失败');
		}

		let max = await this.model().query('select count(id) as max from `think_or_guest_vipgrade` where departmentid="'+orid+'"');
		if (max[0].max > 5) {
			return this.fail('新增会员等级不能超过6条，请自行修改等级！');
		}

		let addata = {
			vipgrade: data.vipgrade,
			goodsdiscount: data.goodsdiscount,
			subjectdiscount: data.subjectdiscount,
			vipname: data.vipname,
			remarks: data.remarks,
			departmentid: orid,
			addtime: addtime,
		}
		let addvip = await this.model('or_guest_vipgrade').add(addata);
		return addvip?this.success('添加成功'):this.fail('添加失败');
	}

	// 删除当前会员等级
	async deletevipAction(){
		let id = this.post('id');
		let del = await this.model('or_guest_vipgrade').where({id: id}).delete();
		return del?this.success('删除成功'):this.fail('删除失败');
	}

}